﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.Domain.Uow;
using AbpZeroTemplate.Authorization;
using AbpZeroTemplate.MultiTenancy;
using AbpZeroTemplate.MultiTenancy.Dto;
using AbpZeroTemplate.Web.Controllers;
using AbpZeroTemplate.Web.Swagger;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace AbpZeroTemplate.Api.Admin.Controllers
{
    /// <summary>租户
    /// </summary>
    [Area("Admin")]
    [Route("Admin/Tenant/[action]")]
    [ApiName("Admin")]
    [RemoteService]
    [AbpMvcAuthorize(AppPermissions.Pages_Tenants)]
    public class TenantController : AbpZeroTemplateControllerBase
    {
        private readonly ITenantAppService _tenantAppService;
        /// <summary>
        /// </summary>
        public TenantController(ITenantAppService tenantAppService)
        {
            _tenantAppService = tenantAppService;
        }

        /// <summary>获取租户
        /// </summary>
        [HttpGet]
        public async Task<PagedResultDto<TenantListDto>> GetTenants(GetTenantsInput input)
        {
            return await _tenantAppService.GetTenants(input);
        }

        /// <summary>创建租户
        /// </summary>
        [HttpPost]
        [AbpMvcAuthorize(AppPermissions.Pages_Tenants_Create)]
        [UnitOfWork(IsDisabled = true)]
        public async Task CreateTenant([FromBody]CreateTenantInput input)
        {
            await _tenantAppService.CreateTenant(input);
        }

        /// <summary>获取编辑的数据
        /// </summary>
        [HttpGet]
        [AbpMvcAuthorize(AppPermissions.Pages_Tenants_Edit)]
        public async Task<TenantEditDto> GetTenantForEdit(EntityDto input)
        {
            return await _tenantAppService.GetTenantForEdit(input);
        }

        /// <summary>修改租户
        /// </summary>
        [HttpPut]
        [AbpMvcAuthorize(AppPermissions.Pages_Tenants_Edit)]
        public async Task UpdateTenant([FromBody]TenantEditDto input)
        {
            await _tenantAppService.UpdateTenant(input);
        }

        /// <summary>删除租户
        /// </summary>
        [HttpDelete]
        [AbpMvcAuthorize(AppPermissions.Pages_Tenants_Delete)]
        public async Task DeleteTenant([FromBody]EntityDto input)
        {
            await _tenantAppService.DeleteTenant(input);
        }

        /// <summary>获取租户特性
        /// </summary>
        [HttpGet]
        [AbpMvcAuthorize(AppPermissions.Pages_Tenants_ChangeFeatures)]
        public async Task<GetTenantFeaturesEditOutput> GetTenantFeaturesForEdit(EntityDto input)
        {
            return await _tenantAppService.GetTenantFeaturesForEdit(input);
        }

        /// <summary>重置租户特性
        /// </summary>
        [HttpPost]
        [AbpMvcAuthorize(AppPermissions.Pages_Tenants_ChangeFeatures)]
        public async Task ResetTenantSpecificFeatures([FromBody]EntityDto input)
        {
            await _tenantAppService.ResetTenantSpecificFeatures(input);
        }

        /// <summary>更新租户特性
        /// </summary>
        [HttpPut]
        [AbpMvcAuthorize(AppPermissions.Pages_Tenants_ChangeFeatures)]
        public async Task UpdateTenantFeatures([FromBody]UpdateTenantFeaturesInput input)
        {
            await _tenantAppService.UpdateTenantFeatures(input);
        }

    }
}
